<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<title>Android代码混淆官方实现方法 - sw926 - 博客园</title>
<link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=gdNkSiQMyBMkpTeO1ex9qW-60UbMszQItnCUpBAT3LQ1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="http://common.cnblogs.com/Skins/ThinkInside/style.css?id=20140110"/>
<link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/sw926/rss"/>
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/sw926/rsd.xml"/>
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/sw926/wlwmanifest.xml"/>
<script src="http://common.cnblogs.com/script/jquery.js" type="text/javascript"></script>  
<script type="text/javascript">var currentBlogApp = 'sw926', cb_enable_mathjax=false;</script>
<script src="/bundles/blog-common.js?v=eXlhU-bqKkymbSfkt4kaWm3xDU3EegYz0YD5G5sO6gk1" type="text/javascript"></script>
</head>
<body>
<a name="top"></a>

<!--done-->
<div id="home">
<div id="header">
	<div id="blogTitle">
	<a id="lnkBlogLogo" href="http://www.cnblogs.com/sw926/"><img id="blogLogo" src="/Skins/custom/images/logo.gif" alt="返回主页" /></a>			
		
<!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/sw926/">sw926</a></h1>
<h2></h2>



		
	</div><!--end: blogTitle 博客的标题和副标题 -->
	<div id="navigator">
		
<ul id="navList">
<li><a id="MyLinks1_HomeLink" class="menu" href="http://www.cnblogs.com/">博客园</a></li>
<li><a id="MyLinks1_MyHomeLink" class="menu" href="http://www.cnblogs.com/sw926/">首页</a></li>
<li><a class="menu" href="http://q.cnblogs.com/">博问</a></li>
<li><a class="menu" href="http://home.cnblogs.com/ing/">闪存</a></li>
<li><a id="MyLinks1_NewPostLink" class="menu" rel="nofollow" href="http://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a></li>
<li><a id="MyLinks1_ContactLink" class="menu" rel="nofollow" href="http://space.cnblogs.com/msg/send/sw926">联系</a></li>
<li><a id="MyLinks1_Syndication" class="menu" href="http://www.cnblogs.com/sw926/rss">订阅</a>
<!--<a id="MyLinks1_XMLLink" class="aHeaderXML" href="http://www.cnblogs.com/sw926/rss"><img src="http://www.cnblogs.com/images/xml.gif" alt="订阅" /></a>--></li>
<li><a id="MyLinks1_Admin" class="menu" rel="nofollow" href="http://i.cnblogs.com/">管理</a></li>
</ul>
		<div class="blogStats">
			
			
<!--done-->
随笔- 15&nbsp;
文章- 0&nbsp;
评论- 33&nbsp;

			
		</div><!--end: blogStats -->
	</div><!--end: navigator 博客导航栏 -->
</div><!--end: header 头部 -->

<div id="main">
	<div id="mainContent">
	<div class="forFlow">
		

<!--done-->
<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/sw926/p/3314165.html">Android代码混淆官方实现方法</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body"><p>首先查看一下 &ldquo;project.properties&rdquo; 这个文件：</p>
<p><span style="font-family: 宋体; font-size: 13px; color: #006400;"># This file is automatically generated by Android Tools.</span><br /><span style="font-family: 宋体; font-size: 13px; color: #006400;"># Do not modify this file -- YOUR CHANGES WILL BE ERASED!</span><br /><span style="font-family: 宋体; font-size: 13px; color: #006400;">#</span><br /><span style="font-family: 宋体; font-size: 13px; color: #006400;"># This file must be checked in Version Control Systems.</span><br /><span style="font-family: 宋体; font-size: 13px; color: #006400;">#</span><br /><span style="font-family: 宋体; font-size: 13px; color: #006400;"># To customize properties used by the Ant build system edit</span><br /><span style="font-family: 宋体; font-size: 13px; color: #006400;"># "ant.properties", and override values to adapt the script to your</span><br /><span style="font-family: 宋体; font-size: 13px; color: #006400;"># project structure.</span><br /><span style="font-family: 宋体; font-size: 13px; color: #006400;">#</span><br /><span style="font-family: 宋体; font-size: 13px; color: #006400; background-color: #ffff99;"># To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):</span><br /><span style="font-family: 宋体; font-size: 13px; color: #006400;">#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt</span></p>
<p><span style="font-family: 宋体; font-size: 13px; color: #006400;"># Project target.</span><br /><span style="font-family: 宋体; font-size: 13px;">target=android-18</span></p>
<p><span style="font-family: 宋体; font-size: small;"><span style="line-height: 19px;">简单翻译一下：</span></span></p>
<p><span style="font-family: 宋体; font-size: small;"><span style="line-height: 19px;">这个文件又android工具自动生成，不要修改这个文件 &mdash;&mdash; 你的更改将会被删除！（这句有点不懂）</span></span></p>
<p><span style="font-family: 宋体; font-size: small;"><span style="line-height: 19px;">这个文件必须在版本控制系统中。</span></span></p>
<p><span style="font-family: 宋体; font-size: small;"><span style="line-height: 19px;">需要修改Ant构建系统的自定义属性的话，编辑<span>"ant.properties"这个文件，重写一些值来适应你的工程结构。（翻译的很水，不要当真）</span></span></span></p>
<p><span style="font-family: 宋体; font-size: small; background-color: #ffff99;"><span style="line-height: 19px;">如果需要使用ProGuard来压缩和混淆代码，将下面这句解除注释（可用的属性有：sdk.dir, user.home）</span></span></p>
<p><span style="font-family: 宋体; font-size: small; background-color: #ffffff;"><span style="line-height: 19px;">翻译完毕...</span></span></p>
<p><span style="font-family: 宋体; font-size: small; background-color: #ffffff;"><span style="line-height: 19px;">到这里应该明白了，下面这行配置就是代码混淆的开关。</span></span></p>
<p><strong><span style="font-family: 宋体; font-size: small; background-color: #ffffff;"><span style="line-height: 19px;">proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt</span></span></strong></p>
<p><span><span style="font-family: verdana, geneva;">proguard-android.txt&nbsp;</span>这个文件在SDK的&nbsp;<span>tools/proguard 文件夹中，是Google事先写好的一个代码混淆配置文件，是一个基础的android代码混淆配置文件。</span></span></p>
<p>继续查看&nbsp;<span>proguard-project.txt这个文件：</span></p>
<p><span style="color: #006400;"># To enable ProGuard in your project, edit project.properties</span><br /><span style="color: #006400;"># to define the proguard.config property as described in that file.</span><br /><span style="color: #006400;">#</span><br /><span style="color: #006400;"># Add project specific ProGuard rules here.</span><br /><span style="color: #006400;"># By default, the flags in this file are appended to flags specified</span><br /><span style="color: #006400;"># in ${sdk.dir}/tools/proguard/proguard-android.txt</span><br /><span style="color: #006400;"># You can edit the include path and order by changing the ProGuard</span><br /><span style="color: #006400;"># include property in project.properties.</span><br /><span style="color: #006400;">#</span><br /><span style="color: #006400;"># For more details, see</span><br /><span style="color: #006400;">#   http://developer.android.com/guide/developing/tools/proguard.html</span></p>
<p><span style="color: #006400;"># Add any project specific keep options here:</span></p>
<p><span style="color: #006400;"># If your project uses WebView with JS, uncomment the following</span><br /><span style="color: #006400;"># and specify the fully qualified class name to the JavaScript interface</span><br /><span style="color: #006400;"># class:</span><br /><span style="color: #006400;">#-keepclassmembers class fqcn.of.javascript.interface.for.webview {</span><br /><span style="color: #006400;">#   public *;</span><br /><span style="color: #006400;">#}</span></p>
<p><span style="color: #000000;">再次秀一下我蹩脚的英语，简单翻译一下：</span></p>
<p><span style="color: #000000;">如果需要使用ProGuard，编辑project.properties文件，根据描述定义proguard.config。</span></p>
<p><span style="color: #000000;">可以在这儿添加具体的代码混淆规则。</span></p>
<p><span style="color: #000000;">默认情况下，这个文件中的标志附加到${sdk.dir}/tools/proguard/proguard-android.txt指定的标志。</span></p>
<p><span style="color: #000000;">你可以通过改变&nbsp;project.properties 中的&nbsp;ProGuard的include属性来编辑路径和指令。</span></p>
<p><span style="color: #000000;">...</span></p>
<p><span style="color: #000000;">更多的信息，请查看http://developer.android.com/guide/developing/tools/proguard.html。</span></p>
<p><span style="color: #000000;">在这儿添加工程的具体的keep options：</span></p>
<p><span style="color: #000000;">...</span></p>
<p><span style="color: #000000;">如果你的工程使用了WebView中的JS，解除下面的注释，并且指定JavaScript接口类的全名。（也就是是忽略js部分，不进行混淆，方法是解除注释，将fqcn.of.javascript.interface.for.webview替换为与JS绑定的类名。）</span></p>
<p>#-keepclassmembers class fqcn.of.javascript.interface.for.webview {<br />#   public *;<br />#}</p>
<p><span>翻译完毕...</span></p>
<p>通过这两个文件的注释可以得出结论：</p>
<p><span style="color: #ff0000;">proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt<span style="color: #000000;"><strong> 有了这一句就启用的代码混淆。</strong></span></span></p>
<p><span style="color: #ff0000;">proguard-android.txt<strong><span style="color: #000000;"> 是基础的Android代码混淆规则，如果需要添加规则，添加到</span></strong>proguard-project.txt</span><span style="color: #000000;"><strong> 中</strong><strong>。</strong></span></p>
<p>如果没有引入第三方的库，一般情况下&nbsp;proguard-android.txt 的规则是够用的，但是如果有第三方的库，在混淆的时候要把第三方的库忽略掉，这是就需要在&nbsp;proguard-project.txt 文件中添加规则。（android.support.** 已经在proguard-android.txt做了处理，所以如果使用了android.support.v4，就不用添加代码混淆的规则了）。</p>
<p>对第三分库的处理，参考：<a href="http://blog.csdn.net/u_xtian/article/details/7495023">http://blog.csdn.net/u_xtian/article/details/7495023</a></p>
<p>具体做法，在&nbsp;proguard-project.txt 添加:</p>
<p>-libraryjars **.jar （声明lib文件）</p>
<p>-dontwarn com.xx.bbb.** （不提示警告）<br />-keep class com.xx.bbb.** { *;} （不进行混淆）</p>
<p>例如百度地图：</p>
<p>-libraryjars libs/baidumapapi_v2_1_3.jar</p>
<p>-dontwarn&nbsp;com.baidu.mapapi.**<br />-dontwarn&nbsp;com.baidu.platform.**<br />-dontwarn&nbsp;com.baidu.location.**<br />-dontwarn&nbsp;com.baidu.vi.**<br />-dontwarn&nbsp;vi.com.gdi.bgl.android.**</p>
<p>-keep class com.baidu.mapapi.** {*; }<br />-keep class com.baidu.platform.** {*; }<br />-keep class com.baidu.location.** {*; }<br />-keep class com.baidu.vi.** {*; }<br />-keep class vi.com.gdi.bgl.android.** {*; }</p>
<p>可能有人很疑惑，不进行代码混淆的方法我知道，但这些包名去那里找啊，其实很简单。打开Android Private Libraries就能看到。</p>
<p><img src="http://images.cnitblog.com/blog/189715/201309/11111049-b04dbbfc29b043d6b10c6f1f637338bd.png" alt="" /></p></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div>


		</div>
		<div class = "postDesc">posted @ <span id="post-date">2013-09-11 11:18</span> <a href='http://www.cnblogs.com/sw926/'>sw926</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>)  <a href ="http://i.cnblogs.com/EditPosts.aspx?postid=3314165" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(3314165);return false;">收藏</a></div>
	</div>
	<script type="text/javascript">var allowComments=true,isLogined=false,cb_blogId=116516,cb_entryId=3314165,cb_blogApp=currentBlogApp,cb_blogUserGuid='301a890d-d7ea-df11-ac81-842b2b196315',cb_entryCreatedDate='2013/9/11 11:18:00';loadViewCount(cb_entryId);</script>
	
</div><!--end: topics 文章、评论容器-->
<a name="!comments"></a><div id="blog-comments-placeholder"></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id="comment_form" class="commentform">
<a name="commentform"></a>
<div id="divCommentShow"></div>
<div id="comment_nav"><span id="span_refresh_tips"></span><a href="javascript:void(0);" id="lnk_RefreshComments" onclick="return RefreshCommentList();">刷新评论</a><a href="#" onclick="return RefreshPage();">刷新页面</a><a href="#top">返回顶部</a></div>
<div id="comment_form_container"></div>
<div class="ad_text_commentbox" id="ad_text_under_commentbox"></div>
<div id="site_nav_under"><a href="http://www.cnblogs.com/" target="_blank" title="程序员的网上家园">博客园首页</a><a href="http://q.cnblogs.com/" target="_blank" title="程序员问答社区">博问</a><a href="http://news.cnblogs.com/" target="_blank" title="IT新闻">新闻</a><a href="http://home.cnblogs.com/ing/" target="_blank">闪存</a><a href="http://job.cnblogs.com/" target="_blank">程序员招聘</a><a href="http://kb.cnblogs.com/" target="_blank">知识库</a></div>
<div id="ad_under_post_holder"></div>
<script type="text/javascript">
var enableGoogleAd = true;
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
fixPostBodyFormat();
loadAdUnderPost();
</script>
<div id="HistoryToday" class="c_ad_block"></div>
<script type="text/javascript">
loadBlogSignature();
LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate);
GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
</script>
<script type="text/javascript">
    $.ajax({ url: 'http://counter.cnblogs.com/blog/post/' + cb_entryId, type: 'get', dataType: 'script', cache: true });
</script>
</div>

	</div><!--end: forFlow -->
	</div><!--end: mainContent 主体内容容器-->

	<div id="sideBar">
		<div id="sideBarMain">
			
<!--done-->
<div class="newsItem">
<h3 class="catListTitle">公告</h3>
	<div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script>
</div>

			<div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script>
			
			<div id="leftcontentcontainer">
				<div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
			</div>
			
		</div><!--end: sideBarMain -->
	</div><!--end: sideBar 侧边栏容器 -->
	<div class="clear"></div>
	</div><!--end: main -->
	<div class="clear"></div>
	<div id="footer">
		
<!--done-->
Copyright &copy;2014 sw926
	</div><!--end: footer -->
</div><!--end: home 自定义的最大容器 -->
</body>
</html>
